


//
//  OauthNetwork.swift
//  mywei
//
//  Created by myd on 15/12/17.
//  Copyright © 2015年 myd1. All rights reserved.
//

import UIKit
import AFNetworking

enum  NETworkError : Int
{
    case emptToKen = -1
    case emptUid = -2

var errordiscription : String{
    get
    {
   switch self {
    
   case NETworkError.emptToKen:
    return "accecc 为空"
   case NETworkError.emptUid:
    return "Uid为空"
    
        }
    }
}
    func error() -> NSError {
   return NSError(domain: errordiscription, code:rawValue , userInfo:["errorDescription":errordiscription])
    }
    
}




class OauthNetwork: NSObject {
    static let sharedinstance = OauthNetwork()
      let redirect_uri = "http://www.baidu.com"
   private  var manager : AFHTTPSessionManager
    private let client_id = "2127380298"
    private let   client_secret  =  "0c5fa20098b1ad2b28fc34d1499ed42c"
    private let   grant_type = "authorization_code"

    // MARK: 网络请求模型初始化请求授权界面
    override  init()
    {
        let url = "https://api.weibo.com/"
        manager = AFHTTPSessionManager(baseURL: NSURL (string: url))
        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
    }
    
    // MARK: 返回授权请求地址
    func oauth() ->NSURL
    {
        let urlstring = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"
    return NSURL(string: urlstring)!
    }
    
    // MARK: 得到code后发送申请者消息appkey等以及回调地址
    func loadcode (code : String , finsh : ErrorFunc) {
        let url = "oauth2/access_token"
        let  par = ["client_id" : client_id ,
            "client_secret" : client_secret,
            "grant_type" : grant_type ,
            "code" : code,
            "redirect_uri" : redirect_uri]
    manager.POST(url, parameters:par, success: { (_, result) -> Void in
        finsh(resuit : result as? [String : AnyObject],Error : nil)
        
        }) { (_, error : NSError) -> Void in
            finsh(resuit : nil,Error : error)
        }
    }
    
    // MARK: 发送获取用户资料请求需要带参access_token 和 uid
    func loaduserinfo(finish : ErrorFunc)
    {
        guard let tok = user1 . loadtoken()?.access_token else{
            let error : NSError = NETworkError.emptToKen . error()
            finish(resuit: nil, Error: error)
       return
        }
        guard let uid = user1.loadtoken()!.uid else
        {
            let error : NSError = NETworkError.emptUid.error()
            finish(resuit: nil, Error: error)
       return
        }

        let dic = ["access_token" : tok,"uid" : uid]
        let urlstring = "https://api.weibo.com/2/users/show.json"
   manager.GET( urlstring, parameters: dic , success: { (_, re) -> Void in
    
    let r = re as? [String : AnyObject]
    
    finish(resuit: r , Error: nil)
    }) { (_, error : NSError) -> Void in
        print(error)
        finish(resuit: nil , Error: error)
        
        }
    }
    
    
    
    
    // MARK: 发送关注的微博信息
    
    func loadweibomessage( since_id : Int , max_id : Int ,result1 : ErrorFunc )
    {
//        let urlstring = "https://api.weibo.com/2/statuses/friends_timeline.json"
    
      guard let tok = user1.loadtoken()?.access_token! else
        {return}
        var dic = [String : AnyObject]()
      dic = ["access_token" : tok]
        if since_id > 0
        {
       dic["since_id"] = since_id
        }else if max_id>0
        {
          dic["max_id"] = max_id-1
        }
        dic["count"] = 20
        let urlstring = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        manager.GET(urlstring, parameters: dic, success: { (_, result) -> Void in
//print(result?.classForCoder)
            result1(resuit: result as? [String : AnyObject], Error: nil)
            }) { (_, error : NSError) -> Void in
                result1(resuit : nil , Error : error)}}
    
    
    func loadweibodata(finsh : ErrorFunc)
    {
        
        let urlstring = "https://api.weibo.com/2/statuses/friends_timeline.json"
        let tok = user1.loadtoken()?.access_token!
        let dic = ["access_token" : tok!]
        manager.GET(urlstring, parameters: dic, success: { (_, result) -> Void in
           
            finsh(resuit: result as? [String : AnyObject], Error: nil)
            }) { (_, error : NSError) -> Void in
        finsh(resuit: nil, Error: error)
        }
    }
    
    }

    

